import DialogStore from ".";
import { message } from 'antd';
import { each } from 'lodash';
import { Button } from 'antd';
import { observable, action } from "mobx";
import { WeaForm } from 'comsMobx';
import API from '../../apis/cuspath';
import { COUNT, CONCAT, SUM, MAX, MIN, AVERAGE, EMPTY, DISCONCAT, FIRETVALUE } from '../../constants/path';
import {WeaLocaleProvider} from "ecCom";
const getLabel = WeaLocaleProvider.getLabel;

export default class CombineSetDialogStore extends DialogStore {
    @observable loading = true;
    @observable forms = [];
    @observable selectedForm = '';
    @observable form = new WeaForm();
    @observable selectedDetail = '';
    constructor() {
        super();
        this.title = getLabel(502643,"汇总设置");
        this.style = { width: 720, height: 520 };
        this.buttons = [<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@kis8vj@cancel`} key="cancel" type="primary" onClick={this.hide}>{getLabel(31129,'取消')}</Button>,
        <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@43v0gp@save`} key="save" type="primary" onClick={this.save}>{getLabel(30986,'保存')}</Button>];
        this.hasScroll = true;
    }
    @action save = () => {
        this.loading = true;
        API.saveCombineSet({
            ...this.params,
            values: JSON.stringify(this.values)
        }).then(() => {
            this.loading = false;
            message.success(getLabel('502231','保存成功'));
            this.hide();
        });
    }
    @action selTitle = (value) => {
        this.title = value
    }
    @action setButtons = (value) =>{
        this.buttons = [<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@kis8vj@cancel`} key="cancel" type="primary" onClick={this.hide}>{value.get('取消')}</Button>,
        <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@43v0gp@save`} key="save" type="primary" onClick={this.save}>{value.get('保存')}</Button>];
       
    }
    @action open = (params) => {
        this.params = {};
        each(params, (value, key) => {
            if (value) {
                this.params[key] = value;
            }
        });
        this.visible = true;
        this.loading = true;
        API.getFormAndFields(this.params).then(this.setFormAndFields);
    }
    @action setFormAndFields = ({ forms }) => {
        this.loading = false;
        this.forms = forms.map(f => ({ ...f, formid: `${f.formid}` }));
        this.form = new WeaForm();
        this.forms.forEach(form => {
            const { details, tables = [], fields } = form;
            this.form.add('', { key: form.formid });
            const field = this.form.$(form.formid);
            const mainFields = details.filter(d => !d.detailTable);
            mainFields.forEach(f => {
                const options = this.getOptions(f);
                const [defvalue] = options.filter(d => d.selected);
                field.add(f.combineType ? f.combineType : defvalue ? defvalue.key : '', { key: f.fieldid });
                const childField = field.$(f.fieldid);
                childField.set('label', '&nbsp;' + f.name);
                childField.set('options', options);
            });


            tables.forEach(table => {
                field.add('', { key: table.detailTable });
                const detailField = field.$(table.detailTable);
                const detialFields = details.filter(d => d.detailTable === table.detailTable);
                const allDetailFileds = fields.filter(d => d.detailTable === table.detailTable).map(field => {
                                                                                return ({
                                                                                    key: field.id+'',
                                                                                    showname: field.name
                                                                                })
                                                                            
                                                                        })
                detailField.add(table.combineType, { key: 'combineType' });
                detailField.add(table.groupField || '', { key: 'groupField' });
                detailField.add(allDetailFileds, {key :'allDetailFileds'});
                detialFields.forEach(f => {
                    const options = this.getOptions(f);
                    const [defvalue] = options.filter(d => d.selected);
                    console.log(defvalue,defvalue.key, "defvalue")
                    detailField.add(f.combineType ? f.combineType : defvalue ? defvalue.key : '', { key: f.fieldid });
                    const childField = detailField.$(f.fieldid);
                    childField.set('label', '&nbsp;' + f.name);
                    childField.set('options', options);
                })
            })

        });
        this.selectedForm = this.forms.length > 0 ? this.forms[0].formid : '';
        this.selectedDetail = this.forms.length > 0 ? ((this.forms[0].tables || [])[0] || {}).detailTable || '' : '';
    }
    @action changeSelectedForm = (selectedForm) => {
        this.selectedForm = selectedForm;
        this.selectedDetail = (((this.forms.filter(d=>d.formid ===selectedForm)||{})[0].tables ||[])[0] || {}).detailTable || '';
        //this.selectedDetail = this.forms.filter(d=>d.formid ===selectedForm)[0].tables[0].detailTable
    }
    @action changeSelectedDetail = (selectedDetail) => {
        this.selectedDetail = selectedDetail;
    }
    get values() {
        const result = {};
        this.form.map(form => {
            const value = {};
            form.map(field => {
                value[field.key] = field.values();
            });
            result[form.key] = value;
        });
        return result;
    }
    getFields = (formid) => {
        return this.fields[formid] || [];
    }
    getOptions = (field) => {
        const { htmltype, type, muti } = field;
        const isNumber = htmltype === 1 && (type === 2 || (type === 3));
        const isSingle = (htmltype === 3 && muti == false) || htmltype ===5;
        return [{
            key: EMPTY,
            showname: 'EMPTY',
            display: true,
            selected: isSingle,
        }, {
            key: CONCAT,
            showname: getLabel('502871','拼接'),
            display: !isNumber && !isSingle,
            selected: !isNumber && !isSingle
        }, {
            key: DISCONCAT,
            showname: getLabel('521495','拼接(去重)'),
            display: !isNumber && !isSingle
        }, {
            key: FIRETVALUE,
            showname: getLabel('521519','首行值'),
            display: isSingle,
        },
        // {
        //     key: COUNT,
        //     showname: getLabel('502872','计数') ,
        //     display: true
        // }, 
        {
            key: SUM,
            showname: getLabel('502873','合计'),
            display: isNumber && !isSingle,
            selected: isNumber && !isSingle
        }, {
            key: MAX,
            showname: getLabel('502874','最大值'),
            display: isNumber && !isSingle
        }, {
            key: MIN,
            showname: getLabel('502875','最小值'),
            display: isNumber && !isSingle
        }, {
            key: AVERAGE,
            showname: getLabel('502876','平均值'),
            display: isNumber && !isSingle
        }].filter(d => d.display);
    }
}